#include <utitl/logger.h>
//#include <iostream>

namespace logging = boost::log;
namespace src = boost::log::sources;
namespace expr = boost::log::expressions;
namespace sinks = boost::log::sinks;
namespace keywords = boost::log::keywords;
namespace attrs = boost::log::attributes;

using namespace logging::trivial;
//using namespace boost::log;

using boost::shared_ptr;

//using namespace std;

namespace ECG
{
namespace Utitl
{
    Logger* Logger::m_logger = NULL;
    
    Logger* Logger::instance()
    {
        if(NULL == m_logger)
        {
            m_logger = new Logger();
        }
        return m_logger;
    }

    void Logger::destroy()
    {
        if(NULL != m_logger)
        {
            delete m_logger;
            m_logger = NULL;
        } 
    }

    Logger::Logger()
    {
        
        sink  = boost::make_shared< text_sink >();
        sink->locked_backend()->add_stream(
                boost::make_shared< std::ofstream >("ecg_server_pro.log"));
        
        shared_ptr< std::ostream > pStream(&std::clog, boost::null_deleter());
        sink->locked_backend()->add_stream(pStream);
        
        sink->set_formatter (
                expr::format("%1%: [%2%]<%3%>(%4%): %5%")
                % expr::attr< unsigned int >("RecordID")
                % expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S.%f")
                % logging::trivial::severity
                % expr::attr< boost::thread::id >("ThreadID")
                % expr::smessage
                );
        
        attrs::named_scope Scope;
        logging::core::get()->add_thread_attribute("Scope", Scope);
        BOOST_LOG_SCOPED_THREAD_TAG("ThreadID", boost::this_thread::get_id());
        
        attrs::counter< unsigned int > RecordID(1);

        logging::core::get()->add_global_attribute("RecordID", RecordID);
        
        logging::core::get()->add_sink(sink);
        logging::add_common_attributes();
    }

    Logger::~Logger()
    {
        sink->flush();
    }

    
}
}